<template>
    <div class="main-table" style="width: 800px">
      <div ref="chart" style="width: 600px; height: 500px"></div>
      <AppNavigation @yearSelected="onYearSelected" />
    </div>
</template>

<script>
import * as echarts from "echarts";
import AppNavigation from "./AppNavigation.vue";

export default {
  name: "RankingView",
  components: {
    AppNavigation,
    // SchoolMap
  },
  data() {
    return {
      selectedChart: 'scoreDistribution', // Default chart
    };
  },
  mounted() {
    this.$nextTick(() => {
    this.fetchChartData(2020);
  });
  },
  methods: {
    async fetchChartData(year) {
        try {
        const response = await fetch(`http://localhost:8080/api/year?y=${year}`);
        const data = await response.json();
        this.updateChart(data);
        }
        catch (error) {
        console.error("Error fetching data:", error);
        }
    },
    updateChart(data) {
    var  result = data.data;
    var xAxis = [];
    var value = [];
    var series = [];
    for(var i = 0;i < result.length;i++){
      xAxis.push(result[i].xaxis);
      value.push(result[i].value);
      series.push(result[i].series);
    }
    let chart = echarts.getInstanceByDom(this.$refs.chart);
      if (!chart) {
        chart = echarts.init(this.$refs.chart);
      }
    const colors = ['#5470C6',  '#EE6666'];
const option = {
  color: colors,
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'line'
    }
  },
  grid: {
    right: '20%'
  },
  toolbox: {
    feature: {
      dataView: { show: true, readOnly: false },
      restore: { show: true },
      saveAsImage: { show: true }
    }
  },
  legend: {
    data: ['排名', '本段人数']
  },
  xAxis: [
    {
      type: 'category',
      axisTick: {
        alignWithLabel: true
      },
      name: '分数', // X 轴名称
      nameLocation: 'middle', // 位置为中间
      nameGap: 30, // 与轴线的距离
      data: xAxis
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '排名',
      position: 'right',
      alignTicks: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[0]
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    },
    {
      type: 'value',
      name: '本段人数',
      position: 'left',
      alignTicks: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[1]
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    }
  ],
  series: [
    {
      name: '排名',
      type: 'line',
      yAxisIndex: 0, // 使用第一个 yAxis
      data: series
    },
    {
      name: '本段人数',
      type: 'line',
      yAxisIndex: 1, // 使用第二个 yAxis
      data: value
    }
  ]
};
    chart.setOption(option);
    },
    onYearSelected(year) {
      this.fetchChartData(year);
    },
    onChartSelected() {
      this.fetchChartData(new Date().getFullYear()); // Fetch data for the current year
    },
  },
};
</script>

  
  <style scoped>
  .main {
    position: relative;
    margin-top: 150px;
    margin-left: 50px;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    width: 70%;
    min-height: 400px;
    border-radius: 5px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    background-color: #f9f9f9;
  }
  
  .main-table {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-width: 600px;
    width: 80%;
    min-height: 400px;
    border-radius: 5px; 
    background-color: #ffffff; 
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); 
    box-sizing: border-box;
    padding: 10px;
  }
  .main-table > img {
    width: 100%;
    border-bottom: 1px solid #ccc;
  }
  .main-stu {
    display: flex;
    flex-direction: column;
    line-height: 1.5;
    flex: 1;
    justify-content: center;
    align-items: center;
    padding: 20px; 
    background-color: #fafafa; 
    border-left: 1px solid #ddd; 
  }
  .main-stu span {
    margin: 0;
    font-size: 16px; 
    color: #333; 
  }
  .main-stu h2 {
    margin-bottom: 10px;
    font-size: 20px; 
    color: #007bff;
  }

  .select-wrapper {
  position: relative;
  display: inline-block;
}

.custom-select {
  appearance: none;
  background-color: #f9f9f9;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
}

.custom-select:focus {
  border-color: #66afe9;
  outline: none;
  box-shadow: 0 0 5px rgba(102, 175, 233, 0.6);
}

.custom-select::after {
  content: '▼';
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  pointer-events: none;
}
  </style>
  